<?php

class TestWakStorage extends UnitTestCase {

    var $cfg=array(
        'storage'=>array(
            'filenames'=>'tests/{{id}}.mockstorable.txt'
            ,'class'=>'MockStorable'
            ,'id_member'=>'id'));

    function setUp() {
        $this->storage=WakStorage::toolFactory('webappkit.storages',$this->cfg);
    }

    /**
     * test de recherche
     * /
    function testSearch() {
        $S=WakStorage::Factory('webappkit.storages',$this->cfg);
        $this->assertTrue($S->has('alpha'));
        $obj=$S->get('alpha');
        $this->assertIsA($obj,'MockStorable');
        // echo '<pre>'; print_r($S);
    }/*

    /**
     * simple sequence
     */
    function testSimpleSequence() {
        // building storage
        $S=WakStorage::toolFactory('webappkit.storages',$this->cfg);
        // building an instance
        $object=new MockStorable(36,'some data');
        // saving
        $this->assertTrue($S->save($object)
            ,"save(instance) should return true");
        $this->assertTrue($S->has(36)
            ,"has(id) should now return true");
        // getting a copy and check
        if (!$copy=$S->get(36)) {
            $this->fail("could not get a copy of inserted object %s");
            return false;
        }
        $this->assertEqual($object,$copy,"get(id) : retrieved copy should equal saved object");
        //$this->dump($object);
        // modifying copy, saving
        $copy->data='data has changed';
        $S->save($copy);
        if (!$copy2=$S->get(36)) {
            $this->fail("could not get a copy of inserted object %s");
            return false;
        }
        $this->assertEqual($copy,$copy2,"retrieved copy should equal saved changes");
        // deleting
        $this->assertTrue($S->delete(36)
            ,"delete(id) should return true");
        $this->assertFalse($S->has(36)
            ,"has(id) should return false after deletion");
    }
}

class MockStorable {

    function MockStorable($id, $data) {
        $this->__construct($id, $data);
    }

    function __construct($id, $data) {
        $this->id=$id;
        $this->data=$data;
    }

    var $id;

    var $data;
}
